/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-08-02
 * Time: 14:20
 */
public class test1 {

    private static final int NUMBER = 200;
    // 定义一把锁
    private static Object lock = new Object();
    // 记录变量
    private static int currentNum = 1;

    public static void main(String[] args) {
        Thread t1 = new Thread(() ->{
            while (currentNum<NUMBER){
                synchronized (lock){
                    // 代表其是偶数
                    while (currentNum%2==0){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("t1:"+currentNum);
                    currentNum++;
                    lock.notify();
                }
            }
        });

        Thread t2 = new Thread(() ->{
            while (currentNum<NUMBER){
                synchronized (lock){
                    // 代表其是奇数
                    while (currentNum%2!=0){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("t2:"+currentNum);
                    currentNum++;
                    lock.notify();
                }
            }
        });

        t1.start();
        t2.start();
    }
}
